<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */
?>
<?php
    $helper = Mage::helper('merchandiser');
    $params = $this->getRequest()->getParams();
    $categoryId  = 0;
    if (isset($params['category_id'])) {
        $categoryId = $params['category_id'];
    }
    $isRuled = $helper->isCurrentCategoryRuled($categoryId);
?>

<form id="search_mini_form" action="<?php echo Mage::helper("adminhtml")->getUrl('adminhtml/merchandiser/search') ?>" method="get" onsubmit="submitMerchandiserSearch(this); return false;">
    <div class="form-search">
        <input id="search" type="text" name="q" value="<?php echo $this->helper('catalogsearch')->getEscapedQueryText() ?>" class="input-text" />
        <input id="store_id" type="hidden" name="store_id" value="<?php echo $this->getRequest()->getParam('store_id') ?>" />
        <input id="asid" type="hidden" name="asid" value="<?php echo $this->getRequest()->getParam('asid'); ?>" />
        <button type="submit" id="search_mini_form.submit" title="<?php echo $helper->quoteEscape($helper->__('Search')) ?>" class="button">
            <span>
                <span><?php echo $helper->__('Search') ?></span>
            </span>
        </button>
        <div id="search_autocomplete" class="search-autocomplete"></div>
    </div>
</form>

<div id="loading-mask">
    <img src="<?php echo $this->getSkinUrl('merchandiser/images/rule-ajax-loader.gif') ?>" alt="<?php echo $helper->quoteEscape($helper->__('Please wait')) ?>" title="<?php echo $helper->quoteEscape($helper->__('Please wait')) ?>" />
    <span><?php echo $helper->__('Searching...') ?></span>
</div>

<div id="search-results" class="search-results"></div>

<script type="text/javascript">
    //<![CDATA[
    Varien.searchFormMerchandiser = Class.create();
    Varien.searchFormMerchandiser.prototype = {
        initialize : function(form, field, emptyText){
            this.form   = $(form);
            this.field  = $(field);
            this.emptyText = emptyText;

            Event.observe(this.form,  'submit', this.submit.bind(this));
            Event.observe(this.field, 'focus', this.focus.bind(this));
            Event.observe(this.field, 'blur', this.blur.bind(this));
            this.blur();
        },

        submit : function(event){
            if (this.field.value == this.emptyText || this.field.value == ''){
                Event.stop(event);
                return false;
            }
            return true;
        },

        focus : function(event){
            if(this.field.value==this.emptyText){
                this.field.value='';
            }
        },

        blur : function(event){
            if(this.field.value==''){
                this.field.value=this.emptyText;
            }
        },

        initAutocomplete : function(url, destinationElement){
            new Ajax.Autocompleter(
                this.field,
                destinationElement,
                url,
                {
                    paramName: this.field.name,
                    method: 'get',
                    minChars: 2,
                    updateElement: this._selectAutocompleteItem.bind(this),
                    onShow : function(element, update) {
                        if(!update.style.position || update.style.position=='absolute') {
                            update.style.position = 'absolute';
                            Position.clone(element, update, {
                                setHeight: false,
                                offsetTop: element.offsetHeight
                            });
                        }
                        Effect.Appear(update,{duration:0});
                    }

                }
            );
        },

        _selectAutocompleteItem : function(element){
            if (element.title){
                var query = element.title;
                var storeid = $('store_id').value;
                var asid = $('asid').value;
                var form = $('search_mini_form');
                var searchjs = new searchJS();
                searchjs.sendAjax(form.action+'?q='+query+'&store_id='+storeid+'&asid='+asid, form.method, 'search-results');
            }
        }
    }

    var searchForm = new Varien.searchFormMerchandiser('search_mini_form', 'search', '<?php echo $helper->__('Search entire store here...') ?>');

    function submitMerchandiserSearch(form) {
        var query = $('search').value;
        var storeid = $('store_id').value;
        var asid = $('asid').value;
        if('<?php echo $isRuled ?>' == 1) {
            alert('<?php echo $helper->jsQuoteEscape($this->__("This category is set to only include products matched by rules")) ?>');
        } else {
            var searchjs = new searchJS();
            searchjs.sendAjax(form.action+'?q='+query+'&store_id='+storeid+'&asid='+asid, form.method, 'search-results');
        }
    }

    function observerAddMassProducts() {
        jQuery('#addMassButton').unbind('click').click(function() {
            var merJSObject = new merchandiserJS();
            merJSObject.addMassProducts('<?php echo $this->getRequest()->getParam('asid'); ?>','<?php echo $helper->getProductinfoUrl() ?>');
        });
    }

    jQuery(document).ready(function () {
        observerAddMassProducts();
        var merJSObject = new merchandiserJS();
        merJSObject.observerRemoveMassProducts();
    });

    function affectResultedProducts() {
        var storeId = $('front_stores').value;
        if(storeId == 0) {
            return;
        }
        $$('li.search-result').each(function(e) {
            if(!e.hasClassName('website-' + storeId)) {
                var elementId = e.id ;
                elementId = elementId.replace('search-s','');
                $(elementId).remove();
                e.down('div.buttons').innerHTML = e.down('div.buttons').innerHTML + '<a><?php echo $helper->jsQuoteEscape($this->__("Product not available in this website")) ?></a>'
            }
        });
    }
    //]]>
</script>
